package cn.iocoder.yudao.module.shop.category.controller.admin;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.datapermission.core.annotation.DataPermission;
import cn.iocoder.yudao.module.shop.category.pojo.po.ShopCategory;
import cn.iocoder.yudao.module.shop.category.pojo.vo.*;
import cn.iocoder.yudao.module.shop.category.service.ShopCategoryService;
import cn.iocoder.yudao.module.shop.info.dal.dataobject.ShopInfoDO;
import cn.iocoder.yudao.module.shop.info.service.ShopInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.*;

@Tag(name = "管理后台 - 行业类目")
@RestController
@RequestMapping("/shop/category")
@Validated
public class ShopCategoryController {

    @Resource
    private ShopCategoryService shopCategoryService;
    @Resource
    private ShopInfoService shopInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建行业类目")
    @PreAuthorize("@ss.hasAnyRoles('super_admin', 'platform_admin', 'tenant_admin')")
    public CommonResult<Long> createCategory(@Valid @RequestBody ShopCategorySaveReqVO createReqVO) {
        return success(shopCategoryService.createCategory(createReqVO));
    }

    @PostMapping("/update")
    @Operation(summary = "更新行业类目")
    @PreAuthorize("@ss.hasAnyRoles('super_admin', 'platform_admin', 'tenant_admin')")
    public CommonResult<Boolean> updateCategory(@Valid @RequestBody ShopCategorySaveReqVO updateReqVO) {
        shopCategoryService.updateCategory(updateReqVO);
        return success(true);
    }

    @PostMapping("/update-deposit")
    @Operation(summary = "更新保证金")
    @PreAuthorize("@ss.hasAnyRoles('super_admin', 'platform_admin', 'tenant_admin')")
    public CommonResult<Boolean> updateDeposit(@Valid @RequestBody ShopCategoryDepositReqVO updateReqVO) {
        shopCategoryService.updateDeposit(updateReqVO);
        return success(true);
    }

    @GetMapping("/delete")
    @Operation(summary = "删除行业类目")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasAnyRoles('super_admin', 'platform_admin', 'tenant_admin')")
    public CommonResult<Boolean> deleteCategory(@RequestParam("id") Long id) {
        shopCategoryService.deleteCategory(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得行业类目")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasAnyRoles('super_admin', 'platform_admin', 'tenant_admin')")
    public CommonResult<ShopCategoryRespVO> getCategory(@RequestParam("id") Long id) {
        ShopCategory category = shopCategoryService.getCategory(id);
        return success(BeanUtils.toBean(category, ShopCategoryRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得行业类目分页")
    @PreAuthorize("@ss.hasPermission('shop:category:query')")
    @DataPermission(enable = false)
    public CommonResult<PageResult<ShopCategoryRespVO>> getCategoryPage(@Valid ShopCategoryPageReqVO pageVO) {
        PageResult<ShopCategory> pageResult = shopCategoryService.getCategoryPage(pageVO);
        return success(BeanUtils.toBean(pageResult, ShopCategoryRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得行业类目列表")
    @PreAuthorize("@ss.hasPermission('shop:category:query')")
    @DataPermission(enable = false)
    public CommonResult<List<ShopCategoryRespVO>> getCategoryList(@Valid ShopCategoryListReqVO reqVO) {
        List<ShopCategory> list = new ArrayList<>();
        Long shopId = getLoginUserLastShopId();
        if (shopId != null && shopId > 0) {
            List<ShopInfoDO> shops = shopInfoService.getInfoList(getLoginUserShopIds());
            Set<Long> parentIds = shops.stream().map(ShopInfoDO::getCategoryId).collect(Collectors.toSet());
            list.addAll(shopCategoryService.getEnableCategoryList(parentIds));
            list.addAll(shopCategoryService.getEnableDescendentCategoryList(parentIds));
        } else if (isSuperAdmin()) {
            list.addAll(shopCategoryService.getCategoryList(reqVO));
        }
        return success(BeanUtils.toBean(list, ShopCategoryRespVO.class));
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获得行业类目简单列表", description = "主要用于下拉列表选择")
    @PreAuthorize("@ss.hasPermission('shop:category:query')")
    @DataPermission(enable = false)
    public CommonResult<List<ShopCategorySimpleRespVO>> getCategorySimpleList(@RequestParam(value = "shopId", required = false) Long shopId) {
        List<ShopCategory> list = new ArrayList<>();
        if (shopId == null || shopId <= 0) {
            shopId = getLoginUserLastShopId();
        }
        if (shopId != null && shopId > 0) {
            ShopCategory shopCategory = shopInfoService.getShopCategory(shopId);
            if (shopCategory != null) {
                list.add(shopCategory);
                list.addAll(shopCategoryService.getEnableDescendentCategoryList(shopCategory.getId()));
            }
        } else if (isSuperAdmin()) {
            list.addAll(shopCategoryService.getAllCategoryList());
        }
        return success(BeanUtils.toBean(list, ShopCategorySimpleRespVO.class));
    }

}
